#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "iic.h"
#define SHT40_ADDR (0x44 << 1)    // SHT40 I2C 地址
#define CMD_MEASURE_HIGH_PRECISION 0xFD // 温度读取命令
uint8_t sht40_buf[6] = {0}; // SHT40数据
void SHT40_Read_TempHumi(float *temperature, float *humidity) {
    HAL_I2C_Master_Transmit(&hi2c1, SHT40_ADDR, (uint8_t[]){CMD_MEASURE_HIGH_PRECISION}, 1, HAL_MAX_DELAY);
    HAL_Delay(10); // 必须等待测量完成
    HAL_I2C_Master_Receive(&hi2c1, SHT40_ADDR, sht40_buf, 6, HAL_MAX_DELAY);

    uint16_t raw_temp = (sht40_buf[0] << 8) | sht40_buf[1];
    uint16_t raw_humi = (sht40_buf[3] << 8) | sht40_buf[4];
	IIC_LOG_INFO("%d-%d\n",sht40_buf[0],sht40_buf[1]);

    *temperature = -45 + 175 * ((float)raw_temp / 65535.0);
    *humidity = 100 * ((float)raw_humi / 65535.0);
}

void get_tempHumi()
{
    float temperature = 0.0f;
    float humidity = 0.0f;

    SHT40_Read_TempHumi(&temperature, &humidity);

    IIC_LOG_INFO("Temperature: %.2f ℃, Humidity: %.2f %%\n", temperature, humidity);
}
